package edu.sctu.zhuyingzhi.month201506.day20150612;

/**
 * Created by user13 on 2015/6/12.
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
public class DayClient {
    public static void testPost() throws IOException {

        URL url = new URL("http://www.baidu.com");
        URLConnection connection = url.openConnection();

        connection.setDoOutput(true);

        OutputStreamWriter out = new OutputStreamWriter(connection
                .getOutputStream(), "8859_1");
        out.write("username=kevin&password=*********"); //post的关键所在！
        // remember to clean up
        out.flush();
        out.close();

        // 一旦发送成功，用以下方法就可以得到服务器的回应：
        String sCurrentLine;
        String sTotalString;
        sCurrentLine = "";
        sTotalString = "";
        InputStream l_urlStream;
        l_urlStream = connection.getInputStream();
        // 传说中的三层包装阿！
        BufferedReader l_reader = new BufferedReader(new InputStreamReader(
                l_urlStream));
        while ((sCurrentLine = l_reader.readLine()) != null) {
            sTotalString += sCurrentLine + "\r\n";
        }
        System.out.println(sTotalString);
    }
    public static void main(String[] args) throws IOException {
        testPost();
    }
}



